package com.bjlytr.license.client.core;

import com.bjlytr.license.client.entity.LicenseVerifyParam;
import com.bjlytr.license.client.util.AppHome;
import com.bjlytr.license.client.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Ledison
 * @date 2021/9/16
 */
@Slf4j
@Component("clientInit")
public class ClientInit implements InitializingBean {

    private final String publicAlias = "publicCert";

    private final String storePass = "public_password1234";


    // 与license-server项目LicenseApp类中对应
    private final String subject = "yoursubject";
    // 生成目录
    private final String OUT_PATH = "/out/license";
    // 证书目录
    private final String LICENSE_FILE = "/license.lic";
    // 公钥目录
    private final String PUB_CERT_FILE = "/publicCerts.keystore";
    // 项目运行目录
    private String APP_ROOT_PATH;

    private void getOsIpAndMac() {
        CustomLicenseManager customLicenseManager = new CustomLicenseManager();
        customLicenseManager.getServerInfos(true);
    }

    private void createOutFileDir() {
        InputStream isLc = null;
        InputStream isPub = null;
        try {
            String resourcePath = "/license";
            Resource resourceLc = new ClassPathResource(resourcePath + LICENSE_FILE);
            Resource resourcePub = new ClassPathResource(resourcePath + PUB_CERT_FILE);
            if(resourceLc.exists()){
                isLc = resourceLc.getInputStream();
            }
            isPub = resourcePub.getInputStream();
            resourcePub.getInputStream();
            String fileDirPath = APP_ROOT_PATH + OUT_PATH;
            File fileDir = new File(fileDirPath);
            if (!fileDir.exists()) {
                fileDir.mkdirs();
            }
            File fileLc = new File(fileDirPath, LICENSE_FILE);
            File filePub = new File(fileDirPath, PUB_CERT_FILE);
            System.out.println("【license路径】" + fileLc.getAbsolutePath());
            if (!fileLc.exists()) {
                if(!resourceLc.exists()){
                    throw new IOException("证书文件不存在，请联系相关人员授权");
                }
                fileLc.createNewFile();
                FileUtil.toFile(isLc, fileLc);
            }
            if (!filePub.exists()) {
                fileLc.createNewFile();
            }
            FileUtil.toFile(isPub, filePub);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(0);
        } finally {
            if (isLc != null) {
                try {
                    isLc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (isPub != null) {
                try {
                    isPub.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void installLicense() {
        log.info("++++++++ 开始安装证书 ++++++++");
        LicenseVerifyParam param = new LicenseVerifyParam();
        param.setSubject(subject);
        param.setPublicAlias(publicAlias);
        param.setStorePass(storePass);
        param.setLicensePath(APP_ROOT_PATH + OUT_PATH + LICENSE_FILE);
        param.setPublicKeysStorePath(APP_ROOT_PATH + OUT_PATH + PUB_CERT_FILE);
        LicenseVerify licenseVerify = new LicenseVerify();
        //安装证书
        licenseVerify.install(param);
        log.info("++++++++ 证书安装结束 ++++++++");
    }

    @Override
    public void afterPropertiesSet() {
        AppHome home = new AppHome(getClass());
        APP_ROOT_PATH = home.getDir().getAbsolutePath();
        getOsIpAndMac();
        createOutFileDir();
        installLicense();
    }
}
